<taglib name="Common\TagLib\Sohtml"/>
<div class="panel crm order-edit-page exfieldset">
    <fieldset>
        <legend>订单资料</legend>
        <form action="{:U('Order/order_confirm_save')}" method="post" id="frmOrderInfo">
            <table width="100%" class="table-form">
                <tr>
                    <td><th>订单编号：</th><td>{$data.serialNo}</td>
                    <td><th>客户姓名：</th><td><input type="text" name="consigneeName" placeholder="客户姓名" size="18" value="{$data.consigneeName}"/></td>
                    <td><th>联系电话：</th><td><input type="text" name="consigneeTelphone" placeholder="电话号码" size="32" value="{$data.consigneeTelphone}" /></td>
                </td></tr>
                <tr>
                    <td><th>邮政编码：</th><td><input type="text" name="consigneeZip" placeholder="邮政编码" size="15" value="{$data.consigneeZip}" /></td>
                    <td colspan="2"><th>收货地址：</th><td><input type="text" name="consigneeAddress" placeholder="收货地址" size="75" value="{$data.consigneeAddress}" /><a id="addr-mgr" href="{:U('Customer/address_list','uid='.$data['buyerID'])}">[管理]</a></td>
                </td></tr>
                <tr>
                    <td><th style="vertical-align: top">核单员备注：</th><td><textarea name="confirmRemark" cols="14" rows="3" placeholder="核单员备注">{$data.confirmRemark}</textarea></td>
                    <td colspan='2'><th style="vertical-align: top">客户备注：</th><td><textarea name="buyerRemark" cols="75" rows="3" placeholder="客户备注(将打印在快递单上)">{$data.buyerRemark}</textarea></td>
                </td></tr>
            </table>
            <br/>
            <if condition="$upr['审核订单']">                
                <input type="button" value="审核通过"/><input type="button" value="退回修改"/>
            </if>
            <input type="button" value="保存数据"/>
            <input type="hidden" name='id' value='{$data.id}'/>
            <input type="hidden" name='siteID' value='{$data.siteID}'/>
            <input type="hidden" name='buyerID' value='{$data.buyerID}' />
            <input type="hidden" name='sellerID' value='{$data.sellerID}' />
            <input type="hidden" name="action" />
        </form>
    </fieldset>
    <fieldset class="order-remarks">
        <legend>备注信息</legend>
        <table width="100%">
            <tr>
                <td><strong>客户：</strong><br />{$data.buyerRemark}</td>
                <td><strong>座席：</strong><br />{$data.sellerRemark}</td>
                <td><strong>核单员：</strong><br />{$data.confirmRemark}</td>
                <td><strong>仓库：</strong><br />{$data.storekeeperRemark}</td>
            </td></tr>
        </table>
    </fieldset>
    <div style="margin:5px 0px;">
        <div style="width:500px; float:left;">
            <fieldset>
                <legend>筛选商品</legend>
                <table width="100%">
                    <tr><td>分类：<dbhelper:catalogselect extags='PRODUCT-CATALOG' id="search-product-catalog" siteid="{$data.siteID}" /></td><td align="right">品名：<input type="text" id="search-product-name" placeholder="商品名称" /></td></td></tr>
                    <tr><td colspan="2" align="right"><button class="btn-search" id="btn-search-product" data-search-url="{:U('Product/product_list_json')}">搜索商品</button></td></td></tr>
                </table>
                <div style="max-height:186px; overflow-y: auto" id="search-product-list">
                    <table class="table-grid">
                        <tr><th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><th>品名</th><th>价格</th><th>库存</th><th>操作</th></td></tr>
                        <!--<tr><td>1</td><td>商品1</td><td>12.00</td><td>30</td><td class="no-padding"><a href="#" class="button">查看</a><a href="#" class="button">介绍</a></td></td></tr>-->
                    </table>
                </div>
            </fieldset>    
        </div>
        <div style="width:300px; float:left; margin-left: 10px;">
            <fieldset>
                <legend>商品明细</legend>
                <table class="table-form">
                    <tr><td>商品名称：<input type="text" id="form-productName" /><input type="hidden" id="form-productID" /></td><td>价格：<input type="text" id="form-productPrice" size="8" /></td></td></tr>
                    <tr><td>折扣：<input type="text" id="form-productDiscount" size="5" value="100" />%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数量：<input type="text" id="form-productNumber" size="5" value="1" /></td><td>小计：<input type="text" id="form-productSubtotal" size="8" value="0.00" /></td></td></tr>
                    <tr><td colspan="2" align="right"><button class="btn-plus" id="btn-add-shoppingcar" data-save-url="{:U('OrderItems/item_save')}">购物车</button></td></td></tr>
                </table>  
                <input type='hidden' id='form-shoppingcar-method' />
            </fieldset>
            <fieldset>
                <legend>购物车<a id="car-clear" href="{:U('OrderItems/clear')}" style="color:#666; padding:0px 2px; font-weight:normal;">[清空]</a><span id="car-summary" data-load-url="{:U('OrderItems/subtotal_json')}">(共<strong>0</strong>项, <strong>0.00</strong>元)</span></legend>
                <div style="max-height:138px; overflow-y: auto" id='shoppingcar-list' data-load-url="{:U('OrderItems/list_json')}">
                    <table class="table-grid">
                        <tr><th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><th>名称</th><th>价格</th><th>数量</th><th>折扣</th><th>小计<th>操作</th></td></tr>
                        <!--<tr data-id="0"><td>1</td><td>商品1</td><td>12.00</td><td>30</td><td>100%</td><td>100</td><td class="no-padding"><a href="#" class="btn-edit lnk-edit1">编辑</a> <a href="#" class="btn-minus lnk-delete1">删除</a></td></td></tr>-->
                    </table>
                </div>
            </fieldset>
        </div>
        <div style="clear: both;"></div>
    </div>
</div>
<input type='hidden' id='oe-product-intro-url' value="{:U('Product/product_intro','id=varid')}" />
<input type='hidden' id='oe-car-delete-url' value="{:U('OrderItems/item_delete')}" />
<script>
    resize_dialog(900);
    $('#addr-mgr').click(function () {
        var opts = {'title': '地址管理', 'width': 580, 'close': function () {
                $(this).remove();
            }};
        $('<div><iframe name="addrmgr" style="width:100%; overflow:hidden;" frameborder="0" scrolling="no" src="' + $(this).attr('href') + '"></iframe></div>').dialog(opts).find('iframe').load(function () {
            var $ifrm = $(this);
            var height = $ifrm.contents().find("body").height();
            $ifrm.height(height).parent().height(height + 10);
        });
        return false;
    });
    // 选择地址 2014-12-15 by sutroon
    function set_address(personalName, telphone, address, zipCode) {
        $('input[name="consigneeName"]').val(personalName);
        $('input[name="consigneeTelphone"]').val(telphone);
        $('input[name="consigneeAddress"]').val(address);
        $('input[name="consigneeZip"]').val(zipCode);
    }
    // 选择商品 2014-12-18 by sutroon
    $('#btn-search-product').click(function () {
        var cat = $('#search-product-catalog').val();
        var name = $('#search-product-name').val();
        load_product_list(cat, name);
    });
    load_product_list('', '');
    // 动态加载商品列表 2014-12-18 by sutroon
    function load_product_list(cat, name) {
        $.post($('#btn-search-product').data('searchUrl'), {'cat': cat, 'name': name, 'limit': 50}, function (data) {
            var str = '<table class="table-grid"><tr><th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><th>品名</th><th>价格</th><th>库存</th><th>操作</th></td></tr>';
            if (!data) {
                $('#search-product-list').html(str + '</table>');
                resize_dialog();
                return;
            }
            for (var i in data) {
                str += '<tr data-id="' + data[i].id + '"><td>' + (i * 1 + 1) + '</td><td class="name">' + data[i].name + '</td><td class="price">' + data[i].price + '</td><td>' + data[i].stock + '</td><td class="no-padding"><a href="#" class="button lnk-view">选择</a><a href="#" class="button lnk-intro" data-id="' + data[i].id + '">介绍</a></td></td></tr>';
            }
            str += '</table>';
            $('#search-product-list').html(str);
            config_button();
            resize_dialog();
        });
    }
    // 按钮点击-查看商品
    $('#search-product-list').delegate('.lnk-view', 'click', function () {
        var $tr = $(this).parent().parent();
        $('#form-productID').val($tr.data('id'));
        $('#form-productName').val($tr.find('.name').text());
        $('#form-productPrice').val($tr.find('.price').text());
        $('#form-productDiscount').val(100);
        $('#form-productNumber').val(1);
        $('#form-productSubtotal').val($tr.find('.price').text());
        $('#form-shoppingcar-method').val('ADD');
        return false;
    });
    // 按钮点击-商品介绍
    $('#search-product-list').delegate('.lnk-intro', 'click', function () {
        var id = $(this).data('id');
        open_dialog($('#oe-product-intro-url').val().replace('varid', id), {'title': '商品详情'});
        return false;
    });
    // 按钮点击-加入购物车 2014-12-18 by sutroon
    $('#btn-add-shoppingcar').click(function () {
        var url = $(this).data('saveUrl');
        var productID = $('#form-productID').val();
        var number = $('#form-productNumber').val();
        var discountRate = $('#form-productDiscount').val();
        var method = $('#form-shoppingcar-method').val();
        var sellerID = $('input[name="sellerID"]').val();
        var orderID = $('input[name="id"]').val();
        if (!id || !number || !discountRate || !method || !sellerID) {
            $.somessager.alert('提示', '商品明细中表单数据不完整!');
            return;
        }
        $.post(url, {'productID': productID, 'number': number, 'discountRate': discountRate, 'sellerID': sellerID, 'orderID': orderID, 'method': method}, function (data) {
            if (data.success == '1') {
                load_shoppingcar();
            } else {
                $.somessager.alert('提示', data.message);
            }
        });
    });
    load_shoppingcar();
    // 加载购物车 2014-12-18 by sutroon
    function load_shoppingcar() {
        var sellerID = $('input[name="sellerID"]').val();
        var orderID = $('input[name="id"]').val();
        $.post($('#shoppingcar-list').data('loadUrl'), {'sellerID': sellerID, 'orderID': orderID}, function (data) {
            var str = '<table class="table-grid"><tr><th class="col-idx"><a href="#" class="btn-arrowstop lnk-multiple" onclick="">&nbsp;</a></th><th>名称</th><th>价格</th><th>数量</th><th>折扣</th><th>小计<th>操作</th></td></tr>';
            if (!data) {
                $('#shoppingcar-list').html(str + '</table>');
                resize_dialog();
                return;
            }
            for (var i in data) {
                str += '<tr data-id="' + data[i].id + '"><td>' + (i * 1 + 1) + '</td><td class="name">' + data[i].productName + '</td><td class="price">' + data[i].price + '</td><td class="number">' + data[i].number + '</td><td class="discount">' + data[i].discountRate + '%</td><td class="subtotal">' + data[i].subtotal + '</td><td class="no-padding"><a href="#" class="btn-edit">编辑</a> <a href="#" class="btn-minus">删除</a></td></td></tr>';
            }
            str += '</table>';
            $('#shoppingcar-list').html(str);
            config_button();
            resize_dialog();
        });
        // 加载购物车小计
        var $summary = $('#car-summary');
        $.get($summary.data('loadUrl'), {'sellerID': sellerID, 'orderID': orderID}, function (data) {
            $summary.html('(共<strong>' + data.count + '</strong>项, <strong>' + data.sum + '</strong>元)');
        });
    }
    // 按钮点击-编辑购物车
    $('#shoppingcar-list').delegate('.btn-edit', 'click', function () {
        var $tr = $(this).parent().parent();
        $('#form-productID').val($tr.data('id'));
        $('#form-productName').val($tr.find('.name').text());
        $('#form-productPrice').val($tr.find('.price').text());
        $('#form-productDiscount').val($tr.find('.discount').text().replace('%', ''));
        $('#form-productNumber').val($tr.find('.number').text());
        $('#form-productSubtotal').val($tr.find('.subtotal').text());
        $('#form-shoppingcar-method').val('UPDATE');
        return false;
    })
    // 按钮点击-删除购物车
    $('#shoppingcar-list').delegate('.btn-minus', 'click', function () {
        var id = $(this).parent().parent().data('id');
        var sellerID = $('input[name="sellerID"]').val();
        var orderID = $('input[name="id"]').val();
        $.get($('#oe-car-delete-url').val(), {'productID': id, 'sellerID': sellerID, 'orderID': orderID}, function (data) {
            if (data.success == '1') {
                load_shoppingcar();
            } else {
                $.somessager.alert('提示', data.message);
            }
        });
        return false;
    });
    // 按钮点击-清空购物车 2014-12-19 by sutroon
    $('#car-clear').click(function () {
        var sellerID = $('input[name="sellerID"]').val();
        var orderID = $('input[name="id"]').val();
        $.get($(this).attr('href'), {'sellerID': sellerID, 'orderID': orderID}, function (data) {
            if (data.success == '1') {
                load_shoppingcar();
            } else {
                $.somessager.alert('提示', '清空失败!');
            }
        });
        return false;
    });

    // 折扣文本框、商品数量文本框 焦点离开时重新统计小计金额 2014-12-18 bysutroon
    $('#form-productDiscount, #form-productNumber').blur(function () {
        var price = $('#form-productPrice').val();
        if (!price) {
            return;
        }
        var discount = $('#form-productDiscount').val();
        var number = $('#form-productNumber').val();
        $('#form-productSubtotal').val(price * number * discount / 100);
    });

    // 提交表单 2015-1-12 by sutroon
    $('#frmOrderInfo :button').click(function () {
        var act = '';
        switch ($(this).val()) {
            case '审核通过':
                act = 'success';
                break;
            case '退回修改':
                act = 'return';
                break;
            case '保存数据':
                act = '';
                break;
        }
        $('#frmOrderInfo input[name="action"]').val(act);
        $('#frmOrderInfo').submit();
    })
</script>